<template>
  <dynamic-form-field
    v-show="!hidden"
    ref="formField"
    :item="field"
    :value="value"
    :readonly="readonly"
    :required="required"
    :params="params"
    :data="data"
    @input="handleInput($event)"
    @change-form-data="changeFormData($event)"
  />
</template>
<script>
import FormUtil from './utils/formUtil'
export default {
  props: {
    field: {
      type: Object,
      required: true
    },
    data: {
      type: Object
    },
    formData: {
      type: Object
    },
    value: {
      type: [String, Number, Boolean, Array, Object, Date]
    },
    rights: { // 设置权限
      type: [String, Object],
      default: 'e'
    },
    isReadonly: Boolean,
    // 扩展参数
    params: Object
  },
  data() {
    return {
      tableData: this.data
    }
  },
  computed: {
    hidden() { // 是否隐藏
      return this.hasHidden()
    },
    readonly() { // 是否只读
      return this.isReadonly ? true : this.rights === 'r'
    },
    required() { // 必填
      // 只读隐藏，都设置必填
      return (this.readonly || this.hidden) ? false : (this.rights === 'b' ? true : this.field.field_options.required || false)
    }
  },
  watch: {
    value() {
      this.tableData = { ...this.data, [this.field.name]: this.value }
      // 值改变进行公式计算
      this.runCalFormula()
      // 值改变进行数据联动
      this.runLinkage()

      this.$emit('cinput', this.tableData)
      if (this.$utils.isNotEmpty(this.formData)) {
        this.$emit('change-form-data', this.formData)
      }
    }
  },
  methods: {
    /**
     * 是否有隐藏
     *  pc 设置移动端隐藏
     */
    hasHidden() {
      return this.$utils.toBoolean(this.field.field_options.mobile, true) ? this.rights === 'h' : true
    },
    // 单个值的修改
    handleInput(val) {
      // 这里没有上报event，直接就是value了
      this.$emit('cinput', { ...this.data, [this.field.name]: val })
    },
    // 表单值的修改
    changeFormData(data) {
      this.$emit('cinput', data)
    },
    /**
     * 进行公式计算
     */
    runCalFormula() {
      const field = this.field
      if (field.isSub) {
        const key = field.code + '.' + field.name
        const formula = this.params.responseFormula[key]
        if (formula) {
          FormUtil.runCalFormula(formula, this.formData, field, field.row)
        }
      } else {
        field.code = (field.code || this.params.code)
        const key = field.code + '.' + field.name
        const formula = this.params.responseFormula[key]

        if (formula) {
          FormUtil.runCalFormula(formula, this.tableData, field)
        }
      }
    },
    /**
     * 进行联动数据
     */
    runLinkage() {
      const field = this.field
      if (field.isSub) {
        const key = field.code + '.' + field.name
        const linkages = this.params.responseLinkages[key]
        if (linkages) {
          FormUtil.runLinkage(linkages, this.formData, field, field.row)
        }
      } else {
        field.code = (field.code || this.params.code)
        const key = field.code + '.' + field.name
        const linkages = this.params.responseLinkages[key]

        if (linkages) {
          FormUtil.runLinkage(linkages, this.tableData, field)
        }
      }
    },
    /**
     * 获得表单验证
     */
    getFormValidator() {
      return this.$refs.formField.$validator
    },
    /**
     *  获得表单验证的第一个错误
     */
    getFirstError() {
      return this.$refs.formField.errors.has(this.field.name) ? this.$refs.formField.errors.first(this.field.name) : ''
    },
    /**
     * 获得表单错误信息
     */
    getFormErrors() {
      return this.$refs.formField.errors
    },
    getEl() {
      return this.$refs.formField.getEl()
    }
  }
}
</script>

